﻿Imports FuncClasses

Public Class CustomerEdit

    Private _db As IDatabase
    Private _usr As User

    Public Sub New()
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        DataContext = New Customer
        CleanAdditionalAddresses()
    End Sub

    Public Sub New(customer As Customer)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        If customer IsNot Nothing Then
            DataContext = customer
            PriceGroupComboBox.SelectedIndex = customer.Preisgruppe
            CleanAdditionalAddresses()
            For Each item In customer.Addresses
                Dim temp As New CustomerEAddresses
                AddHandler temp.RemoveAddress, AddressOf RemoveEAddress
                temp.DataContext = item
                lbl_noeadresses.Visibility = Windows.Visibility.Collapsed
                StackPanelEAddresses.Children.Add(temp)
            Next
        Else
            CleanAdditionalAddresses
        End If
    End Sub

    Private Sub CustomerAddAddress_Click(sender As Object, e As RoutedEventArgs) Handles CustomerAddAddress.Click
        Try
            Dim item As New CustomerEAddresses
            Dim c = DirectCast(DataContext, Customer)
            c.Addresses.Add(New EAddress())
            AddHandler item.RemoveAddress, AddressOf RemoveEAddress
            item.DataContext = c.Addresses.Last
            lbl_noeadresses.Visibility = Windows.Visibility.Collapsed
            StackPanelEAddresses.Children.Add(item)
        Catch ex As Exception
            Log.AddError(ex.ToString)
        End Try
    End Sub

    Private Sub RemoveEAddress(item As EAddress, control As CustomerEAddresses)
        Try
            Dim c = DirectCast(DataContext, Customer)
            c.Addresses.Remove(item)
            StackPanelEAddresses.Children.Remove(control)
            If StackPanelEAddresses.Children.Count = 1 Then
                lbl_noeadresses.Visibility = Windows.Visibility.Visible
            End If
        Catch ex As Exception
            Log.AddError(ex.ToString)
        End Try
    End Sub

    Private Sub UserControl_DataContextChanged_1(sender As Object, e As DependencyPropertyChangedEventArgs)
        If DataContext IsNot Nothing Then
            If TypeOf DataContext Is Customer Then
                PriceGroupComboBox.SelectedIndex = DirectCast(DataContext, Customer).Preisgruppe
                CleanAdditionalAddresses()
                For Each item In DirectCast(DataContext, Customer).Addresses
                    Dim temp As New CustomerEAddresses
                    AddHandler temp.RemoveAddress, AddressOf RemoveEAddress
                    temp.DataContext = item
                    lbl_noeadresses.Visibility = Windows.Visibility.Collapsed
                    StackPanelEAddresses.Children.Add(temp)
                Next
            End If
        Else
            CleanAdditionalAddresses()
        End If
    End Sub

    Private Sub CleanAdditionalAddresses()
        Do While StackPanelEAddresses.Children.Count > 1
            StackPanelEAddresses.Children.RemoveAt(1)
        Loop
    End Sub

    Public Sub Save(ByRef cvm As CustomerViewModel)
        FocusTBParentForMVVM(textbox)
        cvm.SaveCustomer(DirectCast(DataContext, Customer))
    End Sub

    Public Sub SetSettingsViewmodel(ByRef svm As VariousSettingsViewModel)
        cb_payment.ItemsSource = svm.PaymentMethodes
    End Sub

    Public Sub SetDatabase(ByRef db As IDatabase)
        _db = db
    End Sub

    Public Sub SetUser(ByRef user As User)
        _usr = user
        PriceGroupComboBox.SelectedIndex = DirectCast(DataContext, Customer).Preisgruppe
    End Sub

    Private Sub FocusTBParentForMVVM(textbox As TextBox)
        Dim ancestor As DependencyObject = textbox.Parent
        While ancestor IsNot Nothing
            Dim element = TryCast(ancestor, UIElement)
            If element IsNot Nothing AndAlso element.Focusable Then
                element.Focus()
                Exit While
            End If
            ancestor = VisualTreeHelper.GetParent(ancestor)
        End While
    End Sub

    Private Sub textbox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles textbox.TextChanged
        If _db.CustomerExists(DirectCast(sender, TextBox).Text, _usr) Then
            DirectCast(sender, TextBox).Foreground = System.Windows.Media.Brushes.Red
        Else
            DirectCast(sender, TextBox).Foreground = System.Windows.Media.Brushes.Black
        End If
    End Sub

    Private Sub PriceGroupChanged() Handles PriceGroupComboBox.SelectionChanged
        If TypeOf DataContext Is Customer Then
            DirectCast(DataContext, Customer).Preisgruppe = PriceGroupComboBox.SelectedIndex
        End If
    End Sub
End Class
